<!-- components/custom-payment/custom-payment.vue -->
<script lang="ts" setup>
  import { ref } from 'vue'

  // 在线支付弹层
  const paymentPopup = ref()

  // 打开弹层
  const open = () => {
    paymentPopup.value.open()
  }
  // 关闭弹层
  const close = () => {
    paymentPopup.value.close()
  }

  // 点击关闭按钮
  const onClickClose = () => {
    // close()
    // 由父组件去做
    emit('close')
  }
  // 点击蒙层
  const onClickMask = () => {
    // close()
    // 由父组件去做
    emit('close')
  }

  // 接收参数
  const props = defineProps<{
    orderId: string
    amount: number
  }>()

  // 开放关闭和显示弹层的方法
  defineExpose({ open, close })

  // ------------- 通过数据渲染画面， 方便后续修改------------
  // 1. 定义支付渠道索引，实现高亮
  const channelIndex = ref(0)
  // 2. 定义支付渠道数据数组
  const channelData = ref([
    {
      name: '微信支付',
      icon: '/static/images/wechatpay-icon.png',
    },
    {
      name: '支付宝支付',
      icon: '/static/images/alipay-icon.png',
    },
  ])
  // 3. 切换高亮方法
  const changeChannel = (index: number) => {
    channelIndex.value = index
  }

  // 4. 子 -> 父通信
  const emit = defineEmits<{
    // 1. 关闭弹层， 不传参
    (e: 'close'): void
    // 2. 确认 - 按钮点击， 传入索引
    (e: 'confirm', index: number): void
  }>()

  // 5. 点击确认按钮
  const onClickConfirm = () => {
    // 1. 获取索引
    const index = channelIndex.value
    // 2. 触发父组件方法 - 传递索引
    emit('confirm', index)
  }
</script>
<template>
  <uni-popup
    :safe-area="false"
    :is-mask-click="false"
    ref="paymentPopup"
    type="bottom"
    @maskClick="onClickMask"
  >
    <view class="payment-container">
      <view class="payment-header">
        <text class="title">选择支付方式</text>
        <uni-icons
          class="uni-icons-close"
          size="18"
          color="#333"
          type="closeempty"
          @click="onClickClose"
        />
      </view>
      <view class="order-amount">¥ {{ props.amount }}.00 </view>
      <uni-list :border="false">
        <uni-list-item
          v-for="(item, index) in channelData"
          :key="index"
          :title="item.name"
          :thumb="item.icon"
          clickable
          @click="changeChannel(index)"
        >
          <template #footer>
            <uni-icons
              v-if="channelIndex == index"
              size="26"
              color="#16C2A3"
              type="checkbox-filled"
            />
            <uni-icons v-else size="26" color="#d1d1d1" type="circle" />
          </template>
        </uni-list-item>
      </uni-list>
      <button class="uni-button" @click="onClickConfirm">立即支付</button>
    </view>
  </uni-popup>
</template>

<style lang="scss">
  .payment-container {
    min-height: 400rpx;
    border-radius: 30rpx 30rpx 0 0;
    background-color: #fff;
    padding: 10rpx 30rpx 40rpx;

    .payment-header {
      height: 88rpx;
      line-height: 88rpx;
      text-align: center;
      margin-bottom: 20rpx;
      font-size: 32rpx;
      color: #333;
      position: relative;
    }

    .uni-icons-close {
      position: absolute;
      top: 2rpx;
      right: 0;
    }

    .order-amount {
      padding: 10rpx 0 10rpx;
      text-align: center;
      font-size: 40rpx;
      color: #333;
    }

    :deep(.uni-list-item__container) {
      padding: 40rpx 0 !important;
    }

    :deep(.uni-list-item--hover) {
      background-color: #fff !important;
    }

    :deep(.uni-list-item__icon) {
      margin-right: 0;
    }

    .uni-button {
      margin-top: 40rpx;
    }
  }
</style>
